package gmp

import (
	"fmt"
	"reflect"
)

type Student struct {
	Age  int
	Name string
}

type Teacher struct {
	Age  int
	Name string
	No   int
}

func (self *Student) CreateSql() string {
	sql := fmt.Sprintf("insert into Student valuds(%d,\"%s\")", self.Age, self.Name)
	return sql
}

type sql interface {
	CreateSql() string
}

func createQuery(q interface{}) string {
	if reflect.ValueOf(q).Kind() == reflect.Struct {
		t := reflect.TypeOf(q).Name()
		query := fmt.Sprintf("insert into %s values(", t)
		v := reflect.ValueOf(q)
		for i := 0; i < v.NumField(); i++ {
			switch v.Field(i).Kind() {
			case reflect.Int:
				if i == 0 {
					query = fmt.Sprintf("%s%d", query, v.Field(i).Int())
				} else {
					query = fmt.Sprintf("%s,%d", query, v.Field(i).Int())
				}
				break
			case reflect.String:
				if i == 0 {
					query = fmt.Sprintf("%s\"%s\"", query, v.Field(i).String())
				} else {
					query = fmt.Sprintf("%s,\"%s\"", query, v.Field(i).String())
				}
				break
			}
		}
		query = fmt.Sprintf("%s)", query)
		//fmt.Println(query)
		return query
	}

	return ""
}
func add(a int, b int) int {
	return a + b
}

//func add2(a int, b int) []int {
//	return []int{a + b}
//}

func add2(a int, b int) (reflect.Type, int) {
	c := a + b
	return reflect.TypeOf(c), c
}

func init3() {
	o := Student{Age: 12, Name: "jonson"}
	t := Teacher{Age: 35, Name: "tom", No: 1}
	fmt.Println(o.CreateSql())
	fmt.Println(createQuery(o))
	fmt.Println(createQuery(t))

	var num float64 = 1.2345
	pointer := reflect.ValueOf(&num)
	value := reflect.ValueOf(num)
	fmt.Println(pointer)
	fmt.Println(value)
	cp := pointer.Interface().(*float64)
	cv := value.Interface().(float64)
	fmt.Println(cp, cv)

	aa := 56
	xx := reflect.ValueOf(&aa).Elem().Int()
	fmt.Println(xx)

	v2 := reflect.ValueOf(add)
	args1 := reflect.ValueOf(2)
	args2 := reflect.ValueOf(3)
	vv := v2.Call([]reflect.Value{args1, args2})
	fmt.Println(vv[0].Interface())

	v22 := reflect.ValueOf(add2)
	args12 := reflect.ValueOf(2)
	args22 := reflect.ValueOf(3)
	vv2 := v22.Call([]reflect.Value{args12, args22})
	fmt.Println(0, vv2[0].Interface())
	fmt.Println(1, vv2[1].Interface())
}
